layui.use(['form', 'table', 'laydate', 'layarea', 'tableSelect'], function () {
    let $ = layui.jquery,
        form = layui.form,
        laydate = layui.laydate,
        table = layui.table,
        layarea = layui.layarea;

    /*下单时间*/
    laydate.render({
        elem: '#update_time',
        type:'date',
        done: function(value, date, endDate){
            submitInfo();
        console.log(value); //得到日期生成的值，如：2017-08-18
        //console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
        //console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
    }
    });
    /*城市级联*/
    layarea.render({
        elem: '#area-picker',
        change: function (res) {
            //选择结果
            submitInfo();
        }
    });
    /*订单列表*/
    table.render({
        elem: '#currentTableId',
        url: '/admin/order/ActionIndex?action=ajax',
        method: 'post',
        parseData: function (res) { //res 即为原始返回的数据
            console.log(res)
            $('#ToSendPrice').html(res.tosendprice);
            $('#ToSendCate').html(res.tosendcate);
            $('#ToSendNum').html(res.tosendnum);
            // return {
            //     "code": res.code, //解析接口状态
            //     "msg": res.msg, //解析提示文本
            //     "count": res.total, //解析数据长度
            //     "data": res.data, //解析数据列表
            // };
        },
        toolbar: '#toolbarDemo',
        defaultToolbar: [ 'exports', 'print'],
        cols: [[
            {type: "checkbox",},
            {field: 'phone', title: '下单手机号' },
            // {field: 'mod_id', title: '23',width:80 },
            {field: 'receive_name', title: '收货人',width:80 },
            {field: 'receive_phone', title: '收货人手机号' },
            {field: 'address_all', title: '地址',width:250},
            {field: 'update_time', title: '下单时间',  },
            {field: 'price', title: '订单金额', },
            {field: 'order_statustitle', title: '订单状态',width:100},
            {title: '操作', toolbar: '#currentTableBar',}
        ]],
        limits: [10, 15, 20, 25, 30, 50, 100],
        limit: 10,
        page: true,
        skin: 'line,row',
        done: function (res, curr, count) {

        }
    });

    table.on('tool(currentTableFilter)', function (obj) {
        let data = obj.data;

        if (obj.event === 'edit') {
            let index = layer.open({
                title: '订单详情',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['80%', '80%'],
                content: '/admin/order/ActionDetail?id=' + data.mod_id,
                end: function () {
                    /*删除session无用图片*/
                    $.ajax({
                        url: '/admin/order/clearSession',
                        type: 'post',
                        dataType: 'json',
                    });
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }

        if (obj.event === 'delete') {
            delinfo(obj.data.mod_id,obj)
            return false
        }

        if(obj.event==='doreceive')
        {
            layer.confirm('确认收货？', function (index) {
                $.ajax({
                    url: '/admin/order/DoReceive',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: obj.data.mod_id,
                        order_status:4
                    },
                    success: function(data) {

                        if (data.status)
                        {
                            layer.msg('修改成功!', {icon: 1, time: 1000},function () {
                                parent.location.reload();
                            });

                        }
                    },
                    error: function(re) {

                        layer.open({
                            title: '修改失败',
                            content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                        })
                    }
                });


                // $(obj)[0].tr.remove()
            });
        }

    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {

        if (obj.event === 'delete') {  // 监听删除操作
            let checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data
                ,id='';
            for(let i=0;i in data;i++)
            {
               id+=data[i].mod_id+','
            }

            id=id.substr(0,id.length-1)

            if(id.length<1){
              layer.alert('请选择要删除的数据')
              return false
            }
            // $(".layui-form-checked").parents('tr').remove()

            delinfo(id)
            return false


        }
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        submitInfo();
    });
    form.on('select(selectInfo)', function (data) {
        submitInfo();
    });

    $("input[name='keywords']").on('blur',function (re) {
        submitInfo();
    })


    /*
    删除订单
     */
    function delinfo(re,obj=false)
    {
        // $(obj)[0].tr.remove()

        layer.confirm('真的删除么', function (index) {
            $.ajax({
                url: '/admin/order/DelInfo',
                type: 'post',
                dataType: 'json',
                data: {
                    id: re,
                },
                success: function(data) {

                    if (data.status) {
                        layer.msg('删除成功!', {icon: 1, time: 1000});
                        if(obj)
                        {
                            $(obj)[0].tr.remove()
                        }else{
                            $(".layui-form-checked").parents('tr').remove()
                        }
                    }else{
                        layer.msg(data.msg, {
                            icon: 2,
                            time: 3000
                        });
                    }
                },
                error: function(re) {

                    layer.open({
                        title: '删除失败',
                        content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                    })
                }
            });


            // $(obj)[0].tr.remove()
        });
    }
    function submitInfo() {
        let param = {};
        let t = $('#formInfo [name]');

        $.each(t, function () {
            param[this.name] = this.value;
        });
        //let result = JSON.stringify(d);

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                //searchParams: result
                param
            }
        }, 'data');
        return false;
    };
});
